require "preload"
require "sdl_const"
ffi.cdef [[
    int SDL_Init(int);
    int SDL_CreateWindow(str,int,int,int,int,int);
    int SDL_Quit();
    int SDL_GL_CreateContext(int);
    int SDL_GL_SwapWindow(int);
    int SDL_Delay(int);
    typedef struct {
            int type ;                  
            int common ;     
            int window ;       
            int key ;       
            int edit ;   
            int text ;     
            int motion ;  
            int button ;  
            int wheel ;  
            int jaxis ;      
            int jball ;      
            int jhat ;        
            int jbutton ;  
            int jdevice ;   
            int caxis ;   
            int cbutton ;
            int cdevice ;
            int adevice ;
            int quit ;           
            int user ;         
            int syswm ;         
            int tfinger ; 
            int mgesture ;
            int dgesture ;
            int drop ;
    } sdlevent;
    int SDL_PollEvent(sdlevent*);
]]

---@class sdl
sdl = class()
local sdlDll = ffi.load("SDL2.dll")

function sdl:ctor(caption, w, h)
    self.pWindow = self:creatWindow(caption, w, h)
    self.sdlevent = ffi.new("sdlevent")
end
---创建窗口
---@param caption 标题
---@param w 宽
---@param h 高
function sdl:creatWindow(caption, w, h)
    w = w or 640
    h = h or 480
    sdlDll.SDL_Init(bit.bor(0x20, 0x4000))
    local pWindow = sdlDll.SDL_CreateWindow(caption, 0x2FFF0000, 0x2FFF0000, w, h, bit.bor(0x00000002, 0x00000004))
    local context = sdlDll.SDL_GL_CreateContext(pWindow)
    return pWindow
end

function sdl:loopMsg()
    while true do
        sdlDll.SDL_PollEvent(self.sdlevent)
        if self.sdlevent.type == _SDL_QUIT then
            sdlDll.SDL_Quit()
            break
        end
        sdlDll.SDL_Delay(14)
    end
end

function sdl:render()
    if self.onRender then
        self:onRender()
    end
    sdlDll.SDL_GL_SwapWindow(self.pWindow)
end

-- local opengl = require "opengl"

-- sdl.SDL_Init(bit.bor(0x20, 0x4000))

-- local pWindow = sdl.SDL_CreateWindow("test鏄暐", 0x2FFF0000, 0x2FFF0000, 640, 480, bit.bor(0x00000002, 0x00000004))
-- local context = sdl.SDL_GL_CreateContext(pWindow)
-- local sdlevent = ffi.new("sdlevent")

-- local r = 0.5
-- local array = {
--     {0, r},
--     {r * math.cos(234 * math.pi / 180), r * math.sin(234 * math.pi / 180)},
--     {r * math.cos(18 * math.pi / 180), r * math.sin(18 * math.pi / 180)},
--     {r * math.cos(162 * math.pi / 180), r * math.sin(162 * math.pi / 180)},
--     {r * math.cos(306 * math.pi / 180), r * math.sin(306 * math.pi / 180)}
-- }

-- local function render()
--     opengl.glClearColor(0.2, 0.2, 0.2, 0)
--     opengl.glClear(bit.bor(0x4000, 0x100))
--     opengl.glBegin(2);
--     for i = 1, #array do
--         local tab = array[i]
--         opengl.glVertex2f(unpack(tab))
--     end
--     opengl.glEnd();
--     sdl.SDL_GL_SwapWindow(pWindow)
-- end

-- render()
-- while true do
--     sdl.SDL_PollEvent(sdlevent)
--     if sdlevent.type == SDL_QUIT then
--         break
--     end
--     sdl.SDL_Delay(14)
-- end

-- sdl.SDL_Quit()
